<?php
// use the dPFramework to have easy database operations (store, delete etc.) by using its ObjectOrientedDesign
// therefore we have to create a child class for the module einstein

// a class named (like this) in the form: module/module.class.php is automatically loaded by the dPFramework

/**
 *	@package dotProject
 *	@subpackage modules
 *	@version $Revision: 1.0 $
*/

// include the powerful parent class that we want to extend for einstein
require_once( $AppUI->getSystemClass ('dp' ) );		// use the dPFramework for easy inclusion of this class here
$status_types = array(  'Resource'=>'Resource',
						'Customer'=>'Customer',
						'Supplier'=>'Supplier',
						'Unit'=>'Unit',
						'Transportation'=>'Transportation',
						'Item'=>'Item',
						'Quotation'=>'Quotation',
						'Reference'=>'Reference'
					  );

/**
 * The Einstein Class
 */
class CMdStatus extends CDpObject {
	// link variables to the einstein object (according to the existing columns in the database table einstein)
	var $status_id = NULL;	//use NULL for a NEW object, so the database automatically assigns an unique id by 'NOT NULL'-functionality
	var $status_code = NULL;
	var $status_name = NULL;
	var $status_type = NULL;
	var $status_active = 1;

	// the constructor of the CEinstein class, always combined with the table name and the unique key of the table
	function CMdStatus() {
		$joins = array(
			array( 'label' => 'Tasks', 'name' => 'tasks', 'idfield' => 'task_id', 'joinfield' => 'task_status' ),
			array( 'label' => 'Unit', 'name' => 'md_unit', 'idfield' => 'unit_id', 'joinfield' => 'unit_status' ),
			array( 'label' => 'Item', 'name' => 'items', 'idfield' => 'item_id', 'joinfield' => 'item_status' ),
			array( 'label' => 'Reference', 'name' => 'projects', 'idfield' => 'project_id', 'joinfield' => 'project_rstatus' ),
			array( 'label' => 'Quotation', 'name' => 'quotations', 'idfield' => 'quote_id', 'joinfield' => 'quote_status' ),
			array( 'label' => 'Transportation', 'name' => 'transportations', 'idfield' => 'trans_id', 'joinfield' => 'trans_status' ),
			array( 'label' => 'Resource', 'name' => 'users', 'idfield' => 'user_id', 'joinfield' => 'user_status' ),
			array( 'label' => 'Customer', 'name' => 'companies', 'idfield' => 'company_id', 'joinfield' => 'company_status' )
		);
		$this->CDpObject( 'md_status', 'status_id', 'status_code', $joins, 'masterdata' );
	}

	function check() {
		if (!isset($this->_tbl_unique) || $this->_tbl_unique == NULL) {
			return NULL;
		}
		$q = new DBQuery();
		$q->addTable("$this->_tbl");
		$q->addWhere("$this->_tbl_key != $this->status_id");
		$q->addWhere("$this->_tbl_unique = '$this->status_code'");
		$result = $q->loadResult();
		if (isset($result) && $result != '') {
			return 'Duplicate status code: ' . $this->status_code;
		}
		return NULL;
	}

 	// trangdt end
	/**
	 * Get type list by of specify type, etc Reference, Quotation, Item...
	 * allow only active = 1 type
	 */
	function getStatusList($type=null){
		return getStatusList($type);
 	}

 	function loadByCode($status_code){
 		$q  = new DBQuery;
		$q->addTable('md_status');
		$q->addQuery('status_id');
		$q->addWhere('status_code = \'' . $status_code . '\'');
		$id = $q->loadResult();
		$this->load($id);
 	}
}
function getStatusList($type=null){
	$q  = new DBQuery;
	$q->addTable('md_status');
	$q->addQuery('status_id, status_name');
	if($type!= null){
		$q->addWhere('status_type =\''.$type.'\'');
	}
	$q->addWhere('status_active = 1 ');
	$sql = $q->prepare();
	$q->clear();
	//db_loadHash($sql, $result);
	$arr = db_loadHashList($sql);
	return arrayMerge(array('' => ''), $arr);
}
?>
